using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace KCD.SharePoint
{
  
    public class CrossSiteLookup : SPFieldText
    {

#region Constructors
        public CrossSiteLookup(SPFieldCollection fields, string fieldName)

            : base(fields, fieldName)
        {
            this.Init();
        }

        public CrossSiteLookup(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
            this.Init();
        } 
#endregion


        private void Init()
        {
            
        }

        

        private static Dictionary<int, string> dictUpdatedPropertyValue = new Dictionary<int, string>();

        private string _delimitedPropertyValues;

        public string DelimitedPropertyValues
        {
            get
            {
                return dictUpdatedPropertyValue.ContainsKey(ContextId) ? dictUpdatedPropertyValue[ContextId] : _delimitedPropertyValues;
            }

            set
            {
                this._delimitedPropertyValues = value;
            }
        }

        public void UpdateMyCustomProperty(string value)
        {
            dictUpdatedPropertyValue[ContextId] = value;
        }

        public int ContextId
        {
            get
            {
                return SPContext.Current.GetHashCode();
            }
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            Update();
        }

        public override void Update()
        {
            if (this.DelimitedPropertyValues != null)
            {
                this.SetCustomProperty("DelimitedPropertyValues", this.DelimitedPropertyValues);
            }
            base.Update();
            if (dictUpdatedPropertyValue.ContainsKey(ContextId))
            dictUpdatedPropertyValue.Remove(ContextId);
        }

        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {
            get
            {
                Microsoft.SharePoint.WebControls.BaseFieldControl LookupControl = new LookupControl();
                LookupControl.FieldName = InternalName;
                return LookupControl;
            }
        }

    }


}
